/*
 * Copyright 2006-2007 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sant.cli;

import org.apache.commons.cli.Option

/**
 * @author Sergey Nebolsin
 */
class OptionBuilder(name:String,description:String) {
  val option = new Option(name, description)

  def create:Option = option
  
  def withLongOpt(newLongopt:String):OptionBuilder = {
    option.setLongOpt(newLongopt)
    this
  }

  def hasArg():OptionBuilder = {
    hasArg(true)
  }

  def hasArg(hasArg:boolean):OptionBuilder = {
    option.setArgs( if(hasArg == true) 1 else Option.UNINITIALIZED );
    this
  }

  def withArgName(name:String):OptionBuilder = {
    option.setArgName(name);
    this
  }

  def isRequired():OptionBuilder = {
    isRequired(true)
  }

  def isRequired(newRequired:boolean):OptionBuilder = {
    option.setRequired(newRequired);
    this
  }

  def withValueSeparator(sep:char):OptionBuilder = {
    option.setValueSeparator(sep);
    this
  }

  def withValueSeparator():OptionBuilder = {
    withValueSeparator('=')
  }


  def hasArgs():OptionBuilder = {
    option.setArgs(Option.UNLIMITED_VALUES);
    this
  }

  def hasArgs(num:Int):OptionBuilder = {
    option.setArgs(num);
    this
  }

  def hasOptionalArg():OptionBuilder = {
    option.setArgs(1);
    option.setOptionalArg(true);
    this
  }

  def hasOptionalArgs():OptionBuilder = {
    option.setArgs(Option.UNLIMITED_VALUES);
    option.setOptionalArg(true);
    this
  }

  def hasOptionalArgs(numArgs:Int):OptionBuilder = {
    option.setArgs(numArgs);
    option.setOptionalArg(true);
    this
  }

  def withType(newType:AnyRef):OptionBuilder = {
    option.setType(newType);
    this
  }

  def withDescription(description:String):OptionBuilder = {
    option.setDescription(description)
    this
  }
}

object OptionBuilder {
  def apply(name:String,description:String){
    new OptionBuilder(name,description)
  }
}
